package net.microlinktech.testing.codec;

/**
 * 
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.charset.Charset;

/**
 * @author gonglei java二进制,字节数组,字符,十六进制,BCD编码转换
 */
public class StringCoder {

	public static final Charset SYSTEM_CODE = Charset.forName("UTF-8");
	
	/**
	 * 十六进制转十进制
	 * @param HexStr
	 * @param Length
	 * @return
	 */
	public static int HexToInt(String HexStr,int Length)   
	{
	    int k = 0;
	    int z ;
	    for ( int i =0; i< Length ;i++)
	    {
	        z = 1;
	        for (int j=0; j < (Length-i-1); j++  )
	            z = z*16 ;
	        switch ( HexStr.substring(i,i+1).toCharArray()[0])
	        {
	          case '0':
	              k  += 0 ;
	              break;
	          case '1':
	              k  +=1 * z;
	              break;
	          case '2':
	              k  +=2 * z;
	              break;
	          case '3':
	              k  +=3 * z;
	              break;
	          case '4':
	              k  +=4 * z;
	              break;
	          case '5':
	              k  +=5 * z;
	              break;
	          case '6':
	              k  +=6 * z;
	              break;
	          case '7':
	              k  +=7 * z;
	              break;
	          case '8':
	              k  +=8 * z;
	              break;
	          case '9':
	              k  +=9 * z;
	              break;
	           case 'a':
	           case 'A':
	              k  +=10 * z;
	              break;
	           case 'b':
	           case 'B':
	              k  +=11 * z;
	              break;
	           case 'c':
	           case 'C':
	              k  +=12 * z;
	              break;
	           case 'd':
	           case 'D':
	              k  +=13 * z;
	              break;
	           case 'e':
	           case 'E':
	              k  +=14 * z;
	              break;
	           case 'f':
	           case 'F':
	              k  +=15 * z;
	              break;
	        }
	    }
	    return k;
	}

	/**
	 * 把16进制字符串转换成字节数组
	 * 
	 * @param hex
	 * @return
	 */
	public static byte[] hexStringToByte(String hex) {
		int len = (hex.length() / 2);
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;
	}

	private static byte toByte(char c) {
		byte b = (byte) "0123456789ABCDEF".indexOf(c);
		if (b == -1) {
			b = (byte) "0123456789abcdef".indexOf(c);
		}
		return b;
	}

	/**
	 * 把字节数组转换成16进制字符串
	 * 
	 * @param bArray
	 * @return
	 */
	public static final String bytesToHexString(byte[] bArray) {
		StringBuilder sb = new StringBuilder(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * 把字节数组转换成16进制字符串
	 * 
	 * @param bArray
	 * @return
	 */
	public static final String bytesToHexString(byte[] bArray, int length, String intervalStr) {
		StringBuilder sb = new StringBuilder(length * 2);
		String sTemp;
		for (int i = 0; i < length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
			sb.append(intervalStr);
		}
		return sb.toString();
	}

	/**
	 * 把字节数组转换成16进制字符串
	 * 
	 * @param bArray
	 * @return
	 */
	public static final String bytesToHexString(byte[] bArray, String intervalStr) {
		StringBuilder sb = new StringBuilder(bArray.length * 2);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
			sb.append(intervalStr);
		}
		return sb.toString();
	}

	public static final String bytesToHexBlankString(byte[] bArray) {
		return bytesToHexString(bArray, " ");
	}


	/**
	 * 把可序列化对象转换成字节数组
	 * 
	 * @param s
	 * @return
	 * @throws IOException
	 */
	public static final byte[] objectToBytes(Serializable s) throws IOException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ObjectOutputStream ot = new ObjectOutputStream(out);
		ot.writeObject(s);
		ot.flush();
		ot.close();
		return out.toByteArray();
	}

	public static final String objectToHexString(Serializable s) throws IOException {
		return bytesToHexString(objectToBytes(s));
	}
	
	public static void main(String[] args) {
		String s = "0A";
		int i = Integer.parseInt(s, 16);
		System.out.println(i);
		byte[] b = hexStringToByte("e0".toUpperCase());
		System.out.println(Integer.toHexString(0xFF & b[0]));
	}

}
